package com.lineage.data.item_etcitem.reel;

import com.lineage.data.cmd.EnchantWeapon;
import com.lineage.data.executor.ItemExecutor;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.L1ItemUpdata;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.S_SystemMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import java.util.Random;

public class AncientsAlchemyScroll extends ItemExecutor {
  public static ItemExecutor get() {
    return new AncientsAlchemyScroll();
  }
  
  public void execute(int[] data, L1PcInstance pc, L1ItemInstance item) {
    int targObjId = data[0];
    L1ItemInstance tgItem = pc.getInventory().getItem(targObjId);
    if (tgItem == null)
      return; 
    if (tgItem.isEquipped()) {
      pc.sendPackets((ServerBasePacket)new S_ServerMessage("(预防误点机制启动)装备中无法强化"));
      return;
    } 
    if (tgItem.isSeal()) {
      pc.sendPackets((ServerBasePacket)new S_SystemMessage(String.valueOf(tgItem.getLogName()) + "处于绑定状态！"));
      return;
    } 
    int safe_enchant = tgItem.getItem().get_safeenchant();
    boolean isErr = false;
    int use_type = tgItem.getItem().getUseType();
    switch (use_type) {
      case 1:
        if (safe_enchant < 0)
          isErr = true; 
        break;
      default:
        isErr = true;
        break;
    } 
    int weaponId = tgItem.getItem().getItemId();
    if (weaponId >= 246 && weaponId <= 255)
      isErr = true; 
    if (tgItem.getBless() >= 128)
      isErr = true; 
    if (weaponId >= 100213 && weaponId <= 100217)
      isErr = true; 
    if (isErr) {
      pc.sendPackets((ServerBasePacket)new S_ServerMessage(79));
      return;
    } 
    int enchant_level = tgItem.getEnchantLevel();
    EnchantWeapon enchantWeapon = new EnchantWeapon();
    int randomELevel = enchantWeapon.randomELevel(tgItem, item.getBless());
    pc.getInventory().removeItem(item, 1L);
    boolean isEnchant = true;
    if (enchant_level < -6) {
      isEnchant = false;
    } else if (enchant_level < safe_enchant) {
      isEnchant = true;
    } else {
      int enchant_level_tmp, enchant_chance_wepon;
      Random random = new Random();
      int rnd = random.nextInt(100) + 1;
      if (safe_enchant == 0) {
        enchant_level_tmp = enchant_level + 6;
      } else {
        enchant_level_tmp = enchant_level;
      } 
      if (enchant_level >= 9) {
        enchant_chance_wepon = (int)L1ItemUpdata.enchant_wepon_up9(enchant_level_tmp);
      } else {
        enchant_chance_wepon = (int)L1ItemUpdata.enchant_wepon_dn9(enchant_level_tmp);
      } 
      if (rnd < enchant_chance_wepon) {
        isEnchant = true;
      } else if (enchant_level >= 9 && rnd < enchant_chance_wepon * 2) {
        randomELevel = 0;
      } else {
        isEnchant = false;
      } 
    } 
    if (randomELevel <= 0 && enchant_level > -6)
      isEnchant = true; 
    if (isEnchant) {
      enchantWeapon.successEnchant(pc, tgItem, randomELevel);
    } else {
      enchantWeapon.failureEnchant(pc, tgItem);
    } 
  }
}
